package com.deyang.workflow.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.deyang.workflow.entity.*;
import com.deyang.workflow.service.*;
import com.deyang.workflow.untils.ResultUtil;
import com.deyang.workflow.vo.TemplateVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 管理后台工单及模板信息处理
 *
 * @author zhanghuaiyu
 */
@RestController
@RequestMapping("/worktemplate")
public class WorkTemplateController {


    @Autowired
    private IWorkOrderBaseService workOrderBaseService;

    @Autowired
    private IWorkOrderDetailsService workOrderDetailsService;

    @Autowired
    private IWorkOrderTemplateService workOrderTemplateService;

    @Autowired
    private IWorkOrderTemplateLogService workOrderTemplateLogService;

    @Autowired
    private IWorkOrderStepService workOrderStepService;

    /**
     * 获取工单信息
     *
     * @return 具体工单信息
     */
    @PostMapping("getWorkOrder")
    public Result getWorkOrder(@RequestBody WorkOrderBase workOrderBase) {
        IPage<WorkOrderBase> workOrder = workOrderBaseService.getWorkOrder(workOrderBase);
        return ResultUtil.success(workOrder);

    }

    /**
     * 获取具体工单编号对应的流转记录
     *
     * @return 具体工单流转记录
     */
    @GetMapping("transferRecord")
    public Result transferRecord(@RequestParam String workOrderId) {
        List<WorkOrderDetails> list = workOrderDetailsService.transferRecord(workOrderId);
        return ResultUtil.success(list);
    }

    /**
     * 获取对应的工单的回显信息
     *
     * @return 返回工单的信息
     */
    @GetMapping("echoWorkOrder")
    public Result echoWorkOrder(@RequestParam String workOrderId) {
        WorkOrderBase workOrderBase = workOrderBaseService.queryWorkOrder(workOrderId);
        List<WorkOrderDetails> details = workOrderDetailsService.transferRecord(workOrderId);
        Map<String, Object> map = new HashMap<>();
        map.put("workOrderBase", workOrderBase);
        map.put("workOrderDetails", details);
        return ResultUtil.success(map);

    }


    /**
     * 工单模板新增
     *
     * @return 返回新增是否成功
     */

    @PostMapping("saveTemplate")
    public Result saveTemplate(@RequestBody TemplateVO templateVO) {
        workOrderTemplateService.saveTemplate(templateVO);
        return ResultUtil.success();
    }


    /**
     * 工单模板查询
     *
     * @return 返回模板表基础
     */
    @PostMapping("queryTemplate")
    public Result queryTemplate(@RequestBody WorkOrderTemplate workOrderTemplate) {

        IPage<WorkOrderTemplate> list = workOrderTemplateService.queryTemplate(workOrderTemplate);
        return ResultUtil.success(list.getRecords());
    }

    /**
     * 模板修改记录
     *
     * @return 返回对应模板id的修改记录
     */
    @GetMapping("queryTemplateLog")
    public Result queryTemplateLog(@RequestParam String templateCode) {
        List<WorkOrderTemplateLog> workOrderTemplateLogs = workOrderTemplateLogService.queryTemplateLog(templateCode);
        return ResultUtil.success(workOrderTemplateLogs);
    }


    /**
     * 编辑模板修改接口
     *
     * @return 返回修改是否
     */

    @PostMapping("editTemplate")
    public Result editTemplate(@RequestBody TemplateVO templateVO) {
        if (null != templateVO) {
            //编辑模板基本信息
            if (null != templateVO.getWorkOrderTemplate()) {
                workOrderTemplateService.editTemplate(templateVO.getWorkOrderTemplate());
            }
            if (null != templateVO.getWorkOrderStepList() && templateVO.getWorkOrderStepList().size() > 0) {
                workOrderStepService.editTemplate(templateVO.getWorkOrderStepList());
            }
        }
        return ResultUtil.success();

    }


    /**
     * 编辑模板回显接口
     *
     * @return 返回修改是否
     */


    @GetMapping("echoTemplate")
    public Result echoTemplate(@RequestParam String templateCode) {
        TemplateVO templateVO = workOrderTemplateService.echoTemplate(templateCode);
        return ResultUtil.success(templateVO);

    }

    /**
     * 人工工单手动派单
     *
     * @return 派单成功
     * @parameter: workOrderId:工单编号 ，templateCode:模板编号
     */
    @GetMapping("sendOrder")
    public Result sendOrder(@RequestParam String workOrderId, @RequestParam String templateCode) {
        workOrderDetailsService.sendOrder(workOrderId, templateCode);
        return ResultUtil.success("人工工单手动派单成功");
    }

}



